home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Editing / Move_Lines_Up.bsh < prev    next >
Text File  |  2013-07-28  |  4KB  |  112 lines

  1. /*
  2. Move_Lines_Down.bsh - Beanshell macro to move a selection of lines down by one
  3. line.  This should handle multiple selections, but doesn't work quite right.
  4.  
  5. Copyright (c) Dale Anson, 2004
  6.  :tabSize=4:indentSize=4:noTabs=false:
  7.  :folding=explicit:collapseFolds=1:
  8.  
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11.  
  12.    1. Redistributions of source code must retain the above copyright notice,
  13.    this list of conditions and the following disclaimer.
  14.    2. Redistributions in binary form must reproduce the above copyright
  15.    notice, this list of conditions and the following disclaimer in the
  16.    documentation and/or other materials provided with the distribution.
  17.    3. The name of the author may not be used to endorse or promote products
  18.    derived from this software without specific prior written permission.
  19.  
  20. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  23. EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  26. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31.  
  32. //Localization
  33. final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");
  34. final static String NoMultipleSelectionError = jEdit.getProperty("macro.rs.MoveLines.NoMultipleSelection.error", "Line move does not work with multiple selection.");
  35.  
  36. // check buffer read-only status
  37. if (buffer.isReadOnly())
  38. {
  39.     Macros.error( view, NotEditableMessage );
  40.     return;
  41. }
  42.  
  43. // get the current selection or the current line if no selection
  44. Selection[] selections = textArea.getSelection();
  45.  
  46. // this doesn't work right with multiple selection, so don't do anything
  47. if (selections.length > 1)
  48. {
  49.     Macros.error( view, NoMultipleSelectionError );
  50.     return;
  51. }
  52.  
  53. int oldCaretPosition = textArea.getCaretPosition();
  54. int lineStart; 
  55. int lineEnd;
  56. if (selections.length == 0)
  57. {
  58.     lineStart = textArea.getCaretLine();
  59.     lineEnd = lineStart;
  60. }
  61. else
  62. {
  63.     lineStart = selections[0].getStartLine();
  64.  
  65.     lineEnd = selections[0].getEndLine();
  66. }
  67.  
  68. if (lineStart == 0)
  69.     return;
  70.  
  71. int end_offset = textArea.getLineEndOffset(lineEnd);
  72.  
  73. int prevLine = lineStart - 1;
  74. int prevLineStartOffset = buffer.getLineStartOffset(prevLine);
  75. String prevLineText = buffer.getLineText(prevLine);
  76. Log.log(Log.DEBUG, this , "prev=" + prevLineText + " endOFfset=" + end_offset);
  77. int prevLineLength = prevLineText.length();
  78.  
  79. boolean lastLine = lineEnd == buffer.getLineCount() - 1;
  80.  
  81. String lineSeparator = buffer.getStringProperty( "lineSeparator" );
  82.  
  83. if (lastLine)
  84.     buffer.insert(end_offset - 1, lineSeparator + prevLineText);
  85. else
  86.  
  87.     buffer.insert(end_offset, prevLineText + lineSeparator);
  88.  
  89. buffer.remove(prevLineStartOffset, prevLineLength + 1);
  90.  
  91. if (selections.length == 0)
  92. {
  93.     textArea.setCaretPosition(oldCaretPosition - prevLineLength - 1);
  94. }
  95.  
  96. Mode mode = buffer.getMode();
  97. String[] indentProps = new String[]{"indentOpenBrackets", "indentOpenBrackets",
  98.     "unalignedOpenBrackets", "unalignedCloseBrackets", "indentNextLine", 
  99.     "unindentThisLine", "electricKeys", "doubleBracketIndent", 
  100.     "lineUpClosingBracket"};
  101. for (String name : indentProps) 
  102. {
  103.     if (mode.getProperty(name) != null)
  104.     {
  105.         buffer.indentLines(lineStart - 1, lineEnd - 1);
  106.         break;
  107.     }
  108. }
  109.  
  110.  
  111.  
  112.